package com.ronri.ams;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.CustomDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static android.content.Context.MODE_PRIVATE;

/**
 * 文件：Test3.java
 * 包名：com.ronri.test;   --- 正式发布时需要修正
 * 说明：轮播组件
 * 作者：MS During
 * 日期：2017/4/12
 * 历史：
 */
public class framentUser extends Fragment {

    private Context mContext = null;
    private SharedPreferences mPref;
    private Unbinder mUnbinder;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View returnView = inflater.inflate(R.layout.frament_user, container, false);

        mContext = getContext();

        mPref = mContext.getSharedPreferences("ronri", MODE_PRIVATE);

        TextView tvUName =  (TextView) returnView.findViewById(R.id.tvUName);
        TextView tvUREAL =  (TextView) returnView.findViewById(R.id.tvUREAL);
        TextView tvName =  (TextView) returnView.findViewById(R.id.tvName);
        TextView tvEmail =  (TextView) returnView.findViewById(R.id.tvEmail);
        TextView tvTelphone =  (TextView) returnView.findViewById(R.id.tvTelphone);
        TextView tvDept =  (TextView) returnView.findViewById(R.id.tvDept);
        TextView tvJGBM =  (TextView) returnView.findViewById(R.id.tvJGBM);
        TextView tvISJS =  (TextView) returnView.findViewById(R.id.tvISJS);

        Button btCancel = (Button) returnView.findViewById(R.id.button);

        tvUName.setText(InitApplication.LoginInfo.getData().getAccount());
        tvName.setText(InitApplication.LoginInfo.getData().getName());
        tvUREAL.setText(InitApplication.LoginInfo.getData().getName());
        tvEmail.setText(InitApplication.LoginInfo.getData().getEmail());
        tvTelphone.setText(InitApplication.LoginInfo.getData().getPhone());
        tvDept.setText(InitApplication.LoginInfo.getData().getDept_name());
        tvJGBM.setText(InitApplication.LoginInfo.getData().getDetector_name());

        if(InitApplication.LoginInfo.getData().getIssample() == 1)
            tvISJS.setText("是");
        else
            tvISJS.setText("否");

        btCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
                builder.setMessage("确认注销当前用户吗？");
                builder.setTitle("信息提示");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        SharedPreferences.Editor editor = mPref.edit();
                        editor.putBoolean("autologin", false);
                        editor.commit();

                        // 关闭当前内容
                        getActivity().finish();

                        // 启动登录
                        Intent intent = new Intent(getActivity(), activity_login.class);
                        startActivity(intent);
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });

                builder.create().show();
            }
        });
        mUnbinder=ButterKnife.bind(this, returnView);
        return returnView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }
}
